<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600753 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="1468"/>

<div>
<span><div><span style="min-height: 10pt; font-family: NSimSun;"><font style="font-size: 14pt;">vector 中insert函数</font></span></div><div><span style="min-height: 10pt; font-family: NSimSun;"><font style="font-size: 14pt;">vector&lt;Point&gt; fpts;</font></span></div><div><span style="min-height: 10pt; font-family: NSimSun;"><font style="font-size: 14pt;">fpts[0].insert(fpts[0].end(),features.begin(), features.end());</font></span></div><div><span style="min-height: 10pt; font-family: NSimSun;"><font style="font-size: 14pt;">第一个参数代表在谁的前面插入值 此时在最后一个插入值 直接插入features全部元素和直接复制区别不大只是思维不同</font></span></div><div><span style="font-family: NSimSun;"><font style="font-size: 14pt;">第二个参数 从哪开始</font></span></div><div><span style="font-family: NSimSun;"><font style="font-size: 14pt;">第三个参数 从哪结束</font></span></div><div><span style="font-size: 9pt; font-family: NSimSun;"><br/></span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>#include &lt;opencv2/opencv.hpp&gt;</div><div>#include &lt;iostream&gt;</div><div>using namespace cv;</div><div>using namespace std;</div><div>Mat frame, gray;</div><div>Mat prev_frame, prev_gray;</div><div>vector&lt;Point2f&gt; features; // shi-tomasi角点检测 - 特征数据</div><div>vector&lt;Point2f&gt; iniPoints; // 初始化特征数据</div><div>vector&lt;Point2f&gt; fpts[2]; // 保持当前帧和前一帧的特征点位置</div><div>vector&lt;uchar&gt; status; // 特征点跟踪成功标志位</div><div>vector&lt;float&gt; errors; // 跟踪时候区域误差和</div><div>void drawFeature(Mat &amp;inFrame);</div><div>void detectFeatures(Mat &amp;inFrame, Mat &amp;ingray);</div><div>void klTrackFeature();</div><div>void drawTrackLines();</div><div>int main(int argc, char** argv) {</div><div>        //VideoCapture capture(0);</div><div>        VideoCapture capture;</div><div>        capture.open(&quot;car.avi&quot;);</div><div>        if (!capture.isOpened()) {</div><div>               printf(&quot;could not load video file...\n&quot;);</div><div>               return -1;</div><div>        }</div><div>        namedWindow(&quot;camera input&quot;, CV_WINDOW_AUTOSIZE);</div><div>        while (capture.read(frame)) {</div><div>               //flip(frame, frame, 1);</div><div>               cvtColor(frame, gray, COLOR_BGR2GRAY);</div><div>               imshow(&quot;gray&quot;,gray);</div><div>               if (fpts[0].size() &lt;40) {</div><div>                       detectFeatures(frame, gray);</div><div>                       fpts[0].insert(fpts[0].end(),features.begin(), features.end());</div><div>                       iniPoints.insert(iniPoints.end(), features.begin(),  features.end());</div><div>               }</div><div>               else {</div><div>                       printf(&quot;tttttttttttttttttttttttttttttttttttttttt...\n&quot;);</div><div>               }</div><div>               if (prev_gray.empty()) {</div><div>                       gray.copyTo(prev_gray);</div><div>               }</div><div>               klTrackFeature();</div><div>               drawFeature(frame);</div><div>               // 更新前一帧数据</div><div>               gray.copyTo(prev_gray);</div><div>               frame.copyTo(prev_frame);</div><div>               imshow(&quot;camera input&quot;, frame);</div><div>               char c = waitKey(50);</div><div>               if (c == 27) {</div><div>                       break;</div><div>               }</div><div>        }</div><div>        waitKey(0);</div><div>        return 0;</div><div>}</div><div>void detectFeatures(Mat &amp;inFrame, Mat &amp;ingray) {</div><div>        double maxCorners = 5000;</div><div>        double qualitylevel = 0.01;</div><div>        double minDistance = 10;</div><div>        double blockSize = 3;</div><div>        double k = 0.04;</div><div>        goodFeaturesToTrack(ingray, features, maxCorners, qualitylevel, minDistance,  Mat(), blockSize, false, k);</div><div>        cout &lt;&lt; &quot;detect features : &quot; &lt;&lt; features.size() &lt;&lt; endl;</div><div>}</div><div>void drawFeature(Mat &amp;inFrame) {</div><div>        for (size_t t = 0; t &lt; fpts[0].size(); t++) {</div><div>               //circle(inFrame, fpts[0][t], 2, Scalar(0, 0, 255), 2, 8, 0);</div><div>        }</div><div>}</div><div>void klTrackFeature() {</div><div>        // KLT</div><div>        calcOpticalFlowPyrLK(prev_gray, gray, fpts[0], fpts[1], status, errors);</div><div>        int k = 0;</div><div>        // 特征点过滤</div><div>        for (int i = 0; i &lt; fpts[1].size(); i++) {</div><div>               double dist = abs(fpts[0][i].x - fpts[1][i].x) + abs(fpts[0][i].y -  fpts[1][i].y);</div><div>               if (dist &gt; 2 &amp;&amp; status[i]) {</div><div>                       iniPoints[k] = iniPoints[i];</div><div>                       fpts[1][k++] = fpts[1][i];</div><div>               }</div><div>        }</div><div>        // 保存特征点并绘制跟踪轨迹</div><div>        iniPoints.resize(k);</div><div>        fpts[1].resize(k);</div><div>        drawTrackLines();</div><div>        std::swap(fpts[1], fpts[0]);</div><div>}</div><div>void drawTrackLines() {</div><div>        for (size_t t = 0; t &lt; fpts[1].size(); t++) {</div><div>               line(frame, iniPoints[t], fpts[1][t], Scalar(0, 255, 0), 1, 8, 0);</div><div>               circle(frame, fpts[1][t], 2, Scalar(0, 0, 255), 2, 8, 0);</div><div>        }</div><div>}</div></div><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">打开一段车辆检测图片</span></div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">转成灰度图</span></div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">进入角点检测</span></div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">对角点检测的预测进行判断不能让他们大于二否则取消预测点</span></div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">其他函数进行画圆等操作</span></div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;"><br/></span></div></div><div><br/></div><div><br/></div></span>
</div></body></html> 